var webpack = require('webpack');

var path = require('path');

var htmlWebpackPlugin = require('html-webpack-plugin') ;

var ExtractTextPlugin  = require('extract-text-webpack-plugin') ;


var autoprefixer = require('autoprefixer');



var baseConfigInit = function (ENV) {

    var pages = require('./webpack.page.config') ;

    var webpackBaseConfig =  {
        entry: {
            common:path.resolve(__dirname, './app/js/common')
        },
        output: {
            filename: '[chunkhash:8].[name].js',
            path: path.resolve(__dirname, 'dist'),
            crossOriginLoading:"anonymous", //  false - 禁用跨域加载（默认） "anonymous" - 不带凭据(credential)启用跨域加载"use-credentials" - 带凭据(credential)启用跨域加载  with credentials

        },
        module:{
            rules:[
                //编译jsx文件
                {
                    test: /\.js?$/,
                    loader: 'babel-loader',
                    exclude: /node_modules/,
                    query: {
                        //这里需要下载 babel-preset-react 依赖才能编译react
                        presets: ['react','es2015','stage-0'],
                        cacheDirectory:true,
                        plugins: [
                          'transform-runtime',
                          'transform-decorators-legacy',
                        ],
                    }
                },
                //加载css、scss文件
                {
                    test: /\.(css|scss)$/,
                    use: ( () => {
                        let loader = ExtractTextPlugin.extract({
                            fallback:"style-loader",
                            use:[
                                'css-loader',
                                'postcss-loader',
                                'sass-loader'
                            ]
                        });
                        return ENV === "DEV" ? ["css-hot-loader"].concat(loader) : loader
                    })()
                },
                {
                    test:/\.(png|jpg|gif)$/,
                    use:  [
                        { loader: "url-loader?limit=8192&name=images/[hash:8].[name].[ext]" }, // limit 字段代表图片打包限制,指当图片大小小于限制时会自动转成 base64 码引用
                    ],
                }
            ]
        },
        resolve: {
            // require时省略的扩展名，如：require('module') 不需要module.js
            extensions: ['.js','.jsx'],
            // 别名，可以直接使用别名来代表设定的路径以及其他
            alias: {
                // filter: path.join(__dirname, './src/filters'),
                // components: path.join(__dirname, './src/components'),
                velocity:"path/to/velocity",
                "velocity-ui": "path/to/velocity.ui"
            }
        },
        plugins:[
            new webpack.optimize.CommonsChunkPlugin({
                name:"common",
                filename: "js/common.js"
            }),
            new ExtractTextPlugin({
                filename: ENV == "DEV"  ? "css/[name].css" : "css/[name]-[chunkhash:8].css"
            }),
            new webpack.ProvidePlugin({
                $: 'jquery',
                // Vue: 'vue',
                React:'react',
                ReactDOM:'react-dom',
                ReactRedux :'react-redux'
            })
        ]
    };
    pages.forEach(function ( item ) {
        //将入口添加到entry中
        webpackBaseConfig.entry[ item.chunk ] = item.viewPath + item.chunk + '.js';

        //按需加载配置
        webpackBaseConfig.plugins.push( new htmlWebpackPlugin({
            title:item.title,
            filename: 'asset/'+item.filename + ".html" ,
            template:  "template.html",
            chunks: [ "common", item.chunk ],
            inject:false
        } ));
    });

    return  webpackBaseConfig ;
};

module.exports =  baseConfigInit ;